package com.splant.android.smart.UILevel;

/**
 * Created by Administrator on 2015/12/28.
 */

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.splant.android.R;

import java.util.ArrayList;
import java.util.List;

public class System_Bluetooth_Discovery_Activity extends Activity implements View.OnClickListener {

    private Handler _handler = new Handler();
    /* 取得默认的蓝牙适配器 */
    private BluetoothAdapter _bluetooth = BluetoothAdapter.getDefaultAdapter();
    /* 用来存储搜索到的蓝牙设备 */
    private List<BluetoothDevice> _devices = new ArrayList<BluetoothDevice>();
    /* 是否完成搜索 */
    private volatile boolean _discoveryFinished;
    private Runnable _discoveryWorkder = new Runnable() {
        public void run() {
            /* 开始搜索 */
            _bluetooth.startDiscovery();
            for (; ; ) {
                if (_discoveryFinished) {
                    break;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
            }
        }
    };
    /**
     * 接收器
     * 当搜索蓝牙设备完成时调用
     */
    private BroadcastReceiver _foundReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            /* 从intent中取得搜索结果数据 */
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
			/* 将结果添加到列表中 */
            _devices.add(device);
			/* 显示列表 */
            deviceList_adapter.notifyDataSetChanged();

        }
    };
    private BroadcastReceiver _discoveryReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
			/* 卸载注册的接收器 */
            unregisterReceiver(_foundReceiver);
            unregisterReceiver(this);
            _discoveryFinished = true;
        }
    };
    private ListView lv_bluetoothDeviceList;
    private System_Bluetooth_DeviceList_Adapter deviceList_adapter;
    private ImageView img_goBack;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        setContentView(R.layout.system_bluetooth_discovery);

        lv_bluetoothDeviceList = (ListView) findViewById(R.id.lv_bluetoothDeviceList);
        img_goBack = (ImageView) findViewById(R.id.img_goBack);
		/* 如果蓝牙适配器没有打开，则结果 */
        if (!_bluetooth.isEnabled()) {
            finish();
            overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
            return;
        }
        deviceList_adapter = new System_Bluetooth_DeviceList_Adapter(this, _devices);
        lv_bluetoothDeviceList.setAdapter(deviceList_adapter);

        lv_bluetoothDeviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                BluetoothDevice bluetoothDevice = _devices.get(i);
                Intent intent = new Intent(System_Bluetooth_Discovery_Activity.this, System_Bluetooth_Main_Activity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable(BluetoothDevice.EXTRA_DEVICE, bluetoothDevice);
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
                overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
            }
        });
		/* 注册接收器 */
        IntentFilter discoveryFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(_discoveryReceiver, discoveryFilter);
        IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(_foundReceiver, foundFilter);
		/* 显示一个对话框,正在搜索蓝牙设备 */
        System_Bluetooth_SamplesUtils.indeterminate(System_Bluetooth_Discovery_Activity.this, _handler, "Scanning...", _discoveryWorkder, new OnDismissListener() {
            public void onDismiss(DialogInterface dialog) {
                for (; _bluetooth.isDiscovering(); ) {
                    _bluetooth.cancelDiscovery();
                }
                _discoveryFinished = true;
            }
        }, true);
        img_goBack.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_goBack:
                finish();
                overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
                break;
        }
    }
}
